﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Data.Xml.Dom;
using Windows.Web.Syndication;

namespace DesdemonaDespairLib
{
    public class Utilities
    {
        public static async Task<SyndicationFeed> GetDesdemonaBlogFeed()
        {
            SyndicationFeed feed = null;

            try
            {
                // Create a syndication client that downloads the feed.  
                SyndicationClient client = new SyndicationClient();
                client.BypassCacheOnRetrieve = true;
                client.SetRequestHeader( customHeaderName, customHeaderValue );

                // Download the feed. 
                feed = await client.RetrieveFeedAsync( new Uri( feedUrl ) );
            }
            catch( Exception ex )
            {
                Debug.WriteLine( ex.ToString() );
            }

            return feed;
        }

        public static async Task< List<DesdemonaSyndicationItem> > GetDesdemonaBlogItems()
        {  
            SyndicationFeed feed = await GetDesdemonaBlogFeed();
            var desItems = feed.Items.Select( i => new DesdemonaSyndicationItem( i ) );

            return desItems.ToList();
        }

        public static string GetFirstParagraph( SyndicationItem item )
        {
            SyndicationContent itemContent = item.Content;
            Uri sourceUri = itemContent.SourceUri;
            string text = itemContent.Text;

            XmlDocument itemDocument = item.GetXmlDocument( SyndicationFormat.Atom10 );


            return null;
        }

        // Although most HTTP servers do not require User-Agent header, others will reject the request or return 
        // a different response if this header is missing. Use SetRequestHeader() to add custom headers. 
        static string customHeaderName = "User-Agent";
        static string customHeaderValue = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";

        //static string textElementName = "text";
        static string feedUrl = @"http://www.desdemonadespair.net/feeds/posts/default";
    }
}
